package com.atguigu.domain.strategy.model.entity;

import com.atguigu.types.common.Constants;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class StrategyRuleEntity {
    private Long strategyId;
    private Integer awardId;
    private Integer ruleType;
    private String ruleModel;
    private String ruleValue;
    private String ruleDesc;

    public Map<String, List<Integer>> getRuleWeightValues(){
        if(!"rule_weight".equals(this.ruleModel)) return null;
        String[] ruleValues = ruleValue.split(Constants.SPACE);
        Map<String, List<Integer>> resultMap = new HashMap<>();
        for (String ruleValue : ruleValues) {
           if(ruleValue==null||ruleValue.isEmpty()){
               return resultMap;
           }
           String[] split = ruleValue.split(Constants.COLON);
           if(split.length!=2){
               throw new IllegalArgumentException("rule_weight rule_rule invalid input format"+ruleValue);
           }
            String[] split1 = split[1].split(Constants.SPLIT);
            ArrayList<Integer> values = new ArrayList<>();
            for (String value : split1) {
                values.add(Integer.parseInt(value));
            }
            resultMap.put(ruleValue,values);
        }
        return resultMap;
    }
}
